<template>
	<view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		onLoad(options) {
			var attId = options.attId //附件id
			var type = options.type // work ｜ cms | bbs
			console.log('下载附件，attId:', attId, ' type: ', type)
			this.o2.showLoading()
			if (!attId || !type) {
			  this.o2.hideLoading()
			  console.log('没有传入参数！')
			  uni.navigateBack()
			} else {
				if (type == 'work') {  // type = work 工作表单的附件
					if (options.workCompleted) { // workCompleted的id
						var downloadUrl = this.o2.Actions.getModuleHost('x_processplatform_assemble_surface') + '/jaxrs/attachment/download/' + attId + '/workcompleted/' + options.workCompleted
						this.downloadFileAndOpen(downloadUrl)
					} else if (options.work) {
						var downloadUrl = this.o2.Actions.getModuleHost('x_processplatform_assemble_surface') + '/jaxrs/attachment/download/' + attId + '/work/' + options.work
						this.downloadFileAndOpen(downloadUrl)
					} else {
					  this.o2.hideLoading()
					  console.log('没有传入工作id！');
					  uni.navigateBack()
					}
				} else if (type == 'cms') {//type = cms cms内容管理表单附件
					var downloadUrl = this.o2.Actions.getModuleHost('x_cms_assemble_control') + '/jaxrs/fileinfo/download/document/' + attId
					this.downloadFileAndOpen(downloadUrl)
				} else if (type == 'bbs') {//type = bbs 论坛下载附件
					var downloadUrl = this.o2.Actions.getModuleHost('x_bbs_assemble_control') + '/jaxrs/attachment/download/' + attId
					this.downloadFileAndOpen(downloadUrl)
				} else {
					this.o2.hideLoading()
					console.log('参数type不正确！type:'+type)
					uni.navigateBack()
				}
			}
		},
		methods: {
			downloadFileAndOpen(url) {
				// #ifdef H5
				console.log('H5端直接打开？')
				this.o2.hideLoading()
				// #endif
				// #ifndef H5
				this.o2.Actions.o2Download(url).then(localUrl=>{
					this.o2.hideLoading()
					uni.openDocument({
					  filePath: localUrl,
					  success: function (res) {
						console.log('打开文档成功');
					  },
					  fail: function (err) {
						console.log(err);
						this.o2.toast('打开文件失败！')
					  }
					});
				}).catch(err => {
					console.log(err)
					this.o2.hideLoading()
					this.o2.toast('下载文件失败！')
				})
				// #endif
			}
		}
	}
</script>

<style>

</style>
